﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using NS.BLL.Systems;
using NS.Model.Systems;

public partial class Systems_MenuManage : System.Web.UI.Page
{
    public string ParentID = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ParentID"] != null)
        {
            ParentID = Request.QueryString["ParentID"];
        }
        if (!Page.IsPostBack)
        {
            GridViewDataBind();
            LabPath.Text = CreatePath(ParentID);
        }
    }


    private string CreatePath(string ParentID)
    {
        StringBuilder MenuPath = new StringBuilder("<a href=MenuManage.aspx?mid=" + Request.QueryString["mid"] + "&ParentID=0>所有菜单</a>>>");
        NS.Model.Systems.Menu Model = MenuService.Select(Convert.ToInt64(ParentID));
        if (Model != null)
        {
            string[] MyArray = (Model.ParentPath + "," + ParentID).Split(',');
            foreach (string MenuID in MyArray)
            {
                if (MenuID != "0")
                {
                    string strMenuName = UtilityService.GetFieldValueByPK("Menu", "MenuName", "MenuID", MenuID)[0].ToString();
                    MenuPath.Append("<a href=MenuManage.aspx?mid=" + Request.QueryString["mid"] + "&ParentID=" + MenuID + ">" + strMenuName + "</a>>>");
                }
            }
        }
        return MenuPath.ToString();
    }

    private void GridViewDataBind()
    {
        FGridView1.SQLString = NS.BLL.Systems.MenuService.GetListStr(ParentID);
        FGridView1.PKey = "MenuID";
        FGridView1.OrderName = "OrderID";
        FGridView1.BindData();
        FGridView1.AddControl();
    }
    protected void btnBindData_Click(object sender, EventArgs e)
    {
        GridViewDataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        NS.Model.Systems.Menu Model = new NS.Model.Systems.Menu();
        Model.MenuName = txtMenuName.Text.Trim(); txtMenuName.Text = string.Empty;
        Model.MenuLink = txtMenuUrl.Text.Trim(); txtMenuUrl.Text = string.Empty;
        Model.OrderID = Convert.ToInt32(string.IsNullOrEmpty(txtOrderID.Text.Trim()) ? "0" : txtOrderID.Text);
        txtOrderID.Text = string.Empty;
        Model.ParentID = Convert.ToInt64(ParentID);
        if (ParentID != "0")
        {
            object[] ParentInfo = UtilityService.GetFieldValueByPK("Menu", "ParentPath,Depth", "MenuID", ParentID.ToString());
            Model.ParentPath = ParentInfo[0].ToString() + "," + ParentID;
            Model.Depth = Convert.ToInt32(ParentInfo[1]) + 1;
        }
        else
        {
            Model.ParentPath = "0";
            Model.Depth = 0;
        }
        if (NS.BLL.Systems.MenuService.Insert(Model) > 0)
        {
            GridViewDataBind();
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string MenuID = ((Button)sender).CommandArgument.ToString();
        NS.BLL.Systems.MenuService.DeleteAll(Convert.ToInt64(MenuID));
        GridViewDataBind();
    }
}